using System;
using Google.API.Translate;

namespace VSTranslate
{
    internal sealed class GoogleTranslateProvider : ITranslateProvider
    {
        #region ITranslateProvider Members

        public TranslateResult Translate(string text, TranslateLanguage languageFrom, TranslateLanguage languageTo)
        {
            try
            {
                //10x2 http://code.google.com/p/google-language-api-for-dotnet/
                string translate = Translator.Translate(
                    text,
                    ConvertLanguage(languageFrom),
                    ConvertLanguage(languageTo),
                    TranslateFormat.text
                    );
                return new TranslateResult(
                    TranslateStatus.Success,
                    translate
                    );
            }
            catch (Exception exception)
            {
                return new TranslateResult(
                    TranslateStatus.Error,
                    exception.Message
                    );
            }
        }

        #endregion

        private static Language ConvertLanguage(TranslateLanguage language)
        {
            switch (language)
            {
                case TranslateLanguage.Russian:
                    return Language.Russian;
                case TranslateLanguage.English:
                    return Language.English;
                default:
                    throw new ArgumentOutOfRangeException("language");
            }
        }
    }
}